package com.qx.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.qx.dao.UsersDao;
import com.qx.entity.Users;
import com.qx.entity.UsersSearch;
import com.qx.service.UsersService;
import com.qx.util.MD5Test2;

@Service
public class UsersImpl implements UsersService {
	@Autowired
	private UsersDao ud;
	
	  @Override 
	  public Page<Users> find(UsersSearch u, Integer page, Integer limit) {
	  if(page==null) page=1; 
	  if(limit==null) limit=10;
	  Specification<Users>spec=getSpec(u); 
	  Pageable p=PageRequest.of(page-1, limit);
	  return ud.findAll(spec, p);
	  }
	  
	  private Specification<Users> getSpec(UsersSearch u) { // TODO Auto-generatedmethod stub 
		  return new Specification<Users>() {
	  
	  @Override 
	  public Predicate toPredicate(Root<Users> root, CriteriaQuery<?>query, CriteriaBuilder criteriaBuilder) { 
		  Predicate p=criteriaBuilder.conjunction();//动态sql表达式 
		  List<Expression<Boolean>> list =p.getExpressions(); 
		  if(!StringUtils.isEmpty(u.getLoginname())) {
	  list.add(criteriaBuilder.like(root.get("loginname"),"%"+u.getLoginname()+"%")); }
		  System.out.println(u.getLoginname());
	  if(!StringUtils.isEmpty(u.getMaxdate())) {
	  list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("mindate"),u.getMaxdate())); 
	  } 
	  if(!StringUtils.isEmpty(u.getMaxdate())) {
	  list.add(criteriaBuilder.lessThanOrEqualTo(root.get("maxdate"),u.getMaxdate())); 
	  } 
	  return p; 
	  }
	  } ; }
	  
	  @Override 
	  public Integer count() { // TODO Auto-generated method stub 
		  return (int) ud.count(); 
	 }
	  
	 

	@Override
	public Users getUsers(String name) {
		Users u=ud.getUsers(name);
		return u;
	}

	@Override
	public Integer save(Users u) {
		String mima=u.getPassword();//拿到密码
		String ss = MD5Test2.generate(mima);//加密密码
		u.setPassword(ss);//赋值密码
		u.setIslockout("否");
		u.setPsdwrongtime("0");
		
		ud.save(u);
		return 0;
	}

	@Override
	public Integer updZhangHu(String z, Integer id) {
		ud.updZhangHu(z, id);
		return 1;
	}

	@Override
	public Integer addUs(Integer userid, Integer roleid) {
		// TODO Auto-generated method stub
		return ud.addUs(userid, roleid);
	}

	@Override
	public Integer delUs(Integer userid, Integer roleid) {
		// TODO Auto-generated method stub
		return ud.delUs(userid, roleid);
	}

	@Override
	public Integer updUsp(Users u) {
		
		String pw="123456";
		String ss = MD5Test2.generate(pw);//密码加密
		u.setPassword(ss);//赋值
		Boolean c=MD5Test2.verify(pw,ss);
		
		return ud.updUsp(u);
	}

	@Override
	public Integer updUsTime(Users u) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		u.setLastlogintime(sdf.format(new Date()));
		return ud.updUsTime(u);
	}

	@Override
	public Integer updUsSuo(Integer id) {
		// TODO Auto-generated method stub
		return ud.updUsSuo(id);
	}

	@Override
	public Integer updUsSuoTimes(Integer id) {
		// TODO Auto-generated method stub
		return ud.updUsSuoTimes(id);
	}

	
	  
	  
	  
	 

}
